home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c
- Subject: Re: Q: pointers to pointers that point to structs?
- Date: 10 Feb 1996 04:03:19 -0500
- Organization: Montclair State University
- Message-ID: <harmon.823942018@pegasus.montclair.edu>
- References: <311C1B4E.217F@mars.superlink.net>
- NNTP-Posting-Host: pegasus.montclair.edu
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
- Michael Rizzo <rizzom@mars.superlink.net> writes:
-
- > I am having trouble dereferencing a pointer to a pointer to a struct.
- >For simplicity just say the struct is:
- >struct test
- > {
- > char teststr[10];
- > int testint;
- > }
-
- >void f(test **temp)
- >{
- >printf("%s %d",(please fill in the blank))
- >}
-
- : printf("%s %d", (*temp)->teststr, (*temp)->testint);
-
- And I'll tell you why (and how). As you are working with a pointer to
- a structure, your assertion about requiring -> over . was correct. Intuitive-
- ly you might then extend the idea to ->->, but at that point you will learn
- that in C, the token immediately to the left of a -> operator must be a
- pointer to a structure (probably thanks to a fine compiler Help system ;) ).
-
- So from **temp, how do we get a pointer? Well, whereas intptr (just making
- this variable up) is a pointer to an int, *intptr is an integer value. So..
- let's call temp testptrptr, for reasons that will be clear in a moment, then
- testptrptr is a pointer to a pointer to a test struct. *testptrptr is a
- pointer to a test struct. (!) **testptrptr is a test struct. So we want one
- asterisk.
-
- Unfortunately (I should say, laid down by the powers that created C), the
- order of operator precedence says -> comes before *, so *temp->testint will
- not work. That's where the parenthesis come in handy, and as the above example
- shows, (*temp)->testint will produce an integer value.
-
- Recap,
-
- >void f(test **temp)
- ^^-- for the first pointer to a struct, always use ->
- \-- for any subsequent pointers, use *, and encircle with ()'s
-
- -- Stone
- --
- # Derek Harmon (aka Stonelight) harmon@pegasus.montclair.edu
- # - Computer Science Undergrad, Montclair State University, NJ
- # - My views are my own, nobody else is this creative. 3;)>
- ... Eschew obfuscation!
-